<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <div class="Mall4j goods-two-items">
    <div
      class="top-header"
      @click="handleHeaderClick"
    >
      <span>{{ config.title }}</span>
      <img
        src="@/assets/images/goods_recommand.png"
        alt="图片加载失败"
      >
    </div>
    <div class="bottom-content">
      <template v-if="config.maingoodsList && config.maingoodsList.length > 0">
        <template
          v-for="(item, index) in config.maingoodsList"
          :key="index"
        >
          <div

            class="left-content"
            @click="handleClick(item)"
          >
            <div class="left-content-img">
              <el-image
                :src="checkFileUrl(item.imgs)"
                fit="fill"
                lazy
              >
                <template #error>
                  <div class="image-slot">
                    <img
                      style="width: 100%"
                      src="@/assets/img/def.png"
                      alt
                    >
                  </div>
                </template>
              </el-image>
              <!-- 下架商品蒙版 start -->
              <div
                v-if="item.status !== 1"
                class="imgs_shelves"
              >
                <img
                  class="been_imgs"
                  src="@/assets/images/been_shelves.png"
                  alt
                >
              </div>
              <!-- 下架商品蒙版 end -->
            </div>
            <div class="title">
              {{ item.name }}{{ item.description }}
            </div>
            <div class="totals">
              <span>￥</span>
              <span>{{ getPrice(item.price, 'left') }}.</span>
              <span>{{ getPrice(item.price, 'right') }}</span>
            </div>
          </div>
        </template>
      </template>
      <template v-else>
        <div class="left-content">
          <el-image
            src=""
            fit="cover"
          >
            <template #error>
              <div class="image-slot">
                <img
                  style="width: 100%"
                  src="@/assets/img/def.png"
                  alt
                >
              </div>
            </template>
          </el-image>
          <div class="title">
            商品名称商品描述
          </div>
          <div class="totals">
            <span>￥</span>
            <span>价格</span>
            <span />
          </div>
        </div>
      </template>
      <div class="right-content">
        <template v-if="config.othergoodsList && config.othergoodsList.length > 0">
          <div class="right-top-content">
            <template
              v-for="(item, index) in config.othergoodsList.slice(0, 3)"
              :key="index"
            >
              <div

                class="right-items"
                @click="handleClick(item)"
              >
                <div class="right-content-img">
                  <el-image
                    :src="checkFileUrl(item.imgs)"
                    fit="fill"
                    lazy
                  >
                    <template #error>
                      <div class="image-slot">
                        <img
                          style="width: 100%"
                          src="@/assets/img/def.png"
                          alt
                        >
                      </div>
                    </template>
                  </el-image>
                  <!-- 下架商品蒙版 start -->
                  <div
                    v-if="item.status !== 1"
                    class="imgs_shelves"
                  >
                    <img
                      class="been_imgs"
                      src="@/assets/images/been_shelves.png"
                      alt
                    >
                  </div>
                  <!-- 下架商品蒙版 end -->
                </div>
                <div class="goods-message">
                  <div class="name">
                    {{ item.name }}{{ item.description }}
                  </div>
                  <div class="totals">
                    <div class="actual">
                      <span>￥</span>
                      <span>{{ getPrice(item.price, 'left') }}.</span>
                      <span>{{ getPrice(item.price, 'right') }}</span>
                    </div>
                    <div class="del-price">
                      ￥{{ getPrice(item.orignPrice, 'left') }}.{{ getPrice(item.orignPrice, 'right') }}
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
          <div class="right-bottom-content">
            <template
              v-for="(item, index) in config.othergoodsList.slice(3)"
              :key="index"
            >
              <div

                class="right-items"
                @click="handleClick(item)"
              >
                <div class="right-content-img">
                  <el-image
                    :src="checkFileUrl(item.imgs)"
                    fit="fill"
                    lazy
                  >
                    <template #error>
                      <div class="image-slot">
                        <img
                          style="width: 100%"
                          src="@/assets/img/def.png"
                          alt
                        >
                      </div>
                    </template>
                  </el-image>
                  <!-- 下架商品蒙版 start -->
                  <div
                    v-if="item.status !== 1"
                    class="imgs_shelves"
                  >
                    <img
                      class="been_imgs"
                      src="@/assets/images/been_shelves.png"
                      alt
                    >
                  </div>
                  <!-- 下架商品蒙版 end -->
                </div>
                <div class="goods-message">
                  <div class="name">
                    {{ item.name }}{{ item.description }}
                  </div>
                  <div class="totals">
                    <div class="actual">
                      <span>￥</span>
                      <span>{{ getPrice(item.price, 'left') }}.</span>
                      <span>{{ getPrice(item.price, 'right') }}</span>
                    </div>
                    <div class="del-price">
                      ￥{{ getPrice(item.orignPrice, 'left') }}.{{ getPrice(item.orignPrice, 'right') }}
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
        </template>
        <template v-else>
          <div class="right-top-content">
            <template
              v-for="index in defaultOtherArr.slice(0, 3).length"
              :key="index"
            >
              <div

                class="right-items"
              >
                <el-image
                  src=""
                  fit="cover"
                >
                  <template #error>
                    <div

                      class="image-slot"
                    >
                      <img
                        style="width: 100%"
                        src="@/assets/img/def.png"
                        alt
                      >
                    </div>
                  </template>
                </el-image>
                <div class="goods-message">
                  <div class="name">
                    商品名称商品描述
                  </div>
                  <div class="totals">
                    <div class="actual">
                      <span>￥</span>
                      <span>价格</span>
                      <span />
                    </div>
                    <div class="del-price">
                      原价
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
          <div class="right-bottom-content">
            <template
              v-for="index in defaultOtherArr.slice(3).length"
              :key="index"
            >
              <div

                class="right-items"
              >
                <el-image
                  src=""
                  fit="cover"
                >
                  <template #error>
                    <div class="image-slot">
                      <img
                        style="width: 100%"
                        src="@/assets/img/def.png"
                        alt
                      >
                    </div>
                  </template>
                </el-image>
                <div class="goods-message">
                  <div class="name">
                    商品名称商品描述
                  </div>
                  <div class="totals">
                    <div class="actual">
                      <span>￥</span>
                      <span>价格</span>
                      <span />
                    </div>
                    <div class="del-price">
                      原价
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>
<script setup>
import { redirectPath } from '../../common/pageConfig.js'
import { ElMessage } from 'element-plus'
import { checkFileUrl } from '@/utils/index.js'

const props = defineProps({
  config: { // 配置信息
    type: Object,
    default: () => {}
  }
})

const defaultOtherArr = new Array(6)

// 得到价格显示
const getPrice = computed(() => {
  return (price, type) => {
    if (!price) return
    const point = price.toString().indexOf('.') // 如果为-1则表示没找到
    let leftPrice
    let rightPrice
    if (point === -1) { // 当前是整数
      leftPrice = price
      rightPrice = '00'
    } else {
      leftPrice = price.toString().slice(0, point)
      rightPrice = price.toString().slice(point + 1)
    }
    switch (type) {
      case 'left':
        return leftPrice
      case 'right':
        return rightPrice
      default:
        break
    }
  }
})

const router = useRouter()
// 点击商品
const handleClick = (item) => {
  if (item.status !== 1) {
    return ElMessage.warning('当前商品已下架')
  }
  const newPage = router.resolve({ path: '/detail', query: { prodId: item.id } })
  window.open(newPage.href, '_blank', 'noopener,noreferrer')
}
// 点击头部
const handleHeaderClick = () => {
  redirectPath(router, props.config)
}

</script>
<style lang="scss" scoped>
@use "index";
</style>
